package com.hjm.project.agentInfo.kouFail.service.impl;

import java.util.List;

import com.hjm.common.exception.BusinessException;
import com.hjm.common.utils.DateUtils;
import com.hjm.common.utils.pay.CkUtils;
import com.hjm.common.utils.security.ShiroUtils;
import org.springframework.stereotype.Service;
import com.hjm.project.agentInfo.kouFail.mapper.KouFailMapper;
import com.hjm.project.agentInfo.kouFail.domain.KouFail;
import com.hjm.project.agentInfo.kouFail.service.IKouFailService;
import com.hjm.common.utils.text.Convert;

import javax.annotation.Resource;

/**
 * 失败记录Service业务层处理
 * 
 * @author maker-platform
 * @date 2021-11-16
 */
@Service
public class KouFailServiceImpl implements IKouFailService {
    @Resource
    private KouFailMapper kouFailMapper;

    /**
     * 查询失败记录
     * 
     * @param id 失败记录ID
     * @return 失败记录
     */
    @Override
    public KouFail selectKouFailById(Integer id) {
        return kouFailMapper.selectKouFailById(id);
    }

    /**
     * 查询失败记录列表
     * 
     * @param kouFail 失败记录
     * @return 失败记录
     */
    @Override
    public List<KouFail> selectKouFailList(KouFail kouFail) {
        return kouFailMapper.selectKouFailList(kouFail);
    }

    /**
     * 查询失败记录列表
     *
     * @param kouFail 失败记录
     * @return 失败记录 一个结果
     */
    @Override
    public KouFail selectKouFailOne(KouFail kouFail) {
        return kouFailMapper.selectKouFailOne(kouFail);
    }

    /**
     * 新增失败记录
     * 
     * @param kouFail 失败记录
     * @return 结果
     */
    @Override
    public int insertKouFail(KouFail kouFail) {
        return kouFailMapper.insertKouFail(kouFail);
    }

    /**
     * 修改失败记录
     * 
     * @param kouFail 失败记录
     * @return 结果
     */
    @Override
    public int updateKouFail(KouFail kouFail) {
        return kouFailMapper.updateKouFail(kouFail);
    }

    /**
     * 删除失败记录对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteKouFailByIds(String ids) {
        return kouFailMapper.deleteKouFailByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除失败记录信息
     * 
     * @param id 失败记录ID
     * @return 结果
     */
    @Override
    public int deleteKouFailById(Integer id) {
        return kouFailMapper.deleteKouFailById(id);
    }


    /**
     * 失败再扣款
     * @Param [params]
     * @Return int
     */
    @Override
    public int saveCk(KouFail kouFail) throws Exception {
        int result = 0;
        //查看当前是否已扣款
        KouFail kouFails = kouFailMapper.selectKouFailById(kouFail.getId());
        //只有0的可扣款
        if ("1".equals(kouFails.getStatus())) {
            throw new BusinessException("不可重复扣款");
        }
        //进行扣款
        try{
            boolean ckResult = CkUtils.sendKou(kouFails.getMoney(), kouFails.getMan(), kouFails.getAgentNum(), kouFails.getAgentName());
            if (ckResult) {
                KouFail updateInfo = new KouFail();
                updateInfo.setId(kouFail.getId());
                updateInfo.setStatus("1");
                updateInfo.setAddMan(ShiroUtils.getLoginName());
                updateInfo.setSendDate(DateUtils.getDateTime());
                result = kouFailMapper.updateKouFail(updateInfo);
            }
        }catch (Exception e){
            KouFail updateInfo = new KouFail();
            updateInfo.setId(kouFail.getId());
            updateInfo.setAddMan(ShiroUtils.getLoginName());
            updateInfo.setSendDate(DateUtils.getDateTime());
            updateInfo.setNote(e.getMessage());
            kouFailMapper.updateKouFail(updateInfo);
            e.printStackTrace();
            throw e;
        }
        return result;
    }
}
